<?php
namespace App\Repository;
use App\Models\Account\CustomerAccountRechargeLog;
use App\Models\Report\Dashboard;

/**
 * 经营概况报表数据仓储类
 *
 * @package App\Http\Repository
 * @author WangBin
 * @mail: <849850352@qq.com>
 */
class DashboardRepository extends BaseRepository
{
    /**
     * 保存数据
     *
     * @param array $data
     * @param int $reportId
     * @throws
     * @return int|mixed
     */
    public function save(array $data, int $reportId = 0)
    {
        $query = $reportId ? Dashboard::query()->find($reportId) : new Dashboard();
        collect($data)->each(function ($value, $field) use ($query) {
            $query->{$field} = $value;
        });
        return $query->save() ? $query->report_id : 0;
    }

    /**
     * 充值记录列表
     *
     * @param array $filter
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getList(array $filter = [])
    {
        $query = CustomerAccountRechargeLog::query()->orderByDesc('created_at');
        if(!empty($filter['customer_id'])) {
            $query->where('customer_id', $filter['customer_id']);
        }
        return $query;
    }

    /**
     * 查询最新的运营看板数据
     *
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function get()
    {
        return Dashboard::query()
            ->orderByDesc('updated_at')
            ->where('status', 2)
            ->first();
    }
}
